package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.patient.service.IPatientInquirerService;
import com.naiterui.ehp.bs.patient.vo.inquirer.InquirerInitVO;
import com.naiterui.ehp.bs.patient.vo.inquirer.InquirerParamVO;
import com.naiterui.ehp.bs.patient.vo.inquirer.InquirerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description 就诊人管理
 * @Author gongbaoqiang
 * @Date 2020/7/8 11:21 AM
 */
@Slf4j
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.PATIENT_INQUIRER})
@RestController
@RequestMapping("ap/patient/inquirer")
public class ApPatientInquirerController {

    @Autowired
    private IPatientInquirerService inquirerService;



    /**
     * 就诊人列表:
     * <pre>
     *     1. 儿科医生时，只能选择18岁以下就诊人
     *     2. 未删除状态
     * </pre>
     * @param patientId
     * @return
     * @throws BusinessException
     */
    @ApiOperation("就诊人列表")
    @ApiImplicitParam(name = "doctorId", value = "医生id", dataType = "long", required = false, paramType = "query")
    @PostMapping("list")
    public List<InquirerVO> list(Long patientId, Long doctorId) throws BusinessException{
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        log.info("就诊人列表：patientId={}, doctorId={}", patientId, doctorId);
        return inquirerService.list(patientId, doctorId);
    }

    /**
     * 添加就诊人
     * <pre>
     *     1. 所属患者就诊人名称唯一
     *     2. 被添加就诊人已删除时，自动找回
     * </pre>
     * @param paramVO
     * @throws BusinessException
     */
    @ApiOperation("添加/编辑 就诊人")
    @PostMapping("add")
    public Long add(@Valid InquirerParamVO paramVO) throws BusinessException {
        if (null == paramVO.getPatientId()) {
            Long patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
            paramVO.setPatientId(patientId);
        }
        log.info("添加/编辑就诊人：paramVO={}", paramVO.toString());
        return inquirerService.saveOrUpdate(paramVO);
    }

    /**
     * 就诊人详情
     *
     * @param inquirerId
     * @return
     * @throws BusinessException
     */
    @ApiOperation("就诊人详情")
    @ApiImplicitParam(name = "inquirerId", value = "就诊人id", dataType = "long", required = true, paramType = "query")
    @PostMapping("detail")
    public InquirerVO detail(@NotNull Long inquirerId, Long patientId) throws BusinessException {
        log.info("就诊人详情：inquirerId={}", inquirerId);
        return inquirerService.detail(patientId, inquirerId);
    }

    /**
     * 删除就诊人
     *
     * @param inquirerId
     * @throws BusinessException
     */
    @ApiOperation("删除就诊人")
    @ApiImplicitParam(name = "inquirerId", value = "就诊人id", dataType = "long", required = true, paramType = "query")
    @PostMapping("del")
    public void del(@NotNull Long inquirerId, Long patientId) throws BusinessException {
        log.info("删除就诊人：inquirerId={}", inquirerId);
        inquirerService.del(patientId, inquirerId);
    }

    /**
     * 就诊人与患者关系
     * @throws BusinessException
     */
    @ApiOperation("初始化就诊人与患者关系")
    @PostMapping("relation/list")
    public InquirerInitVO relationList(Long patientId) throws BusinessException{
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        return inquirerService.relationList(patientId);
    }

}
